performancingUtil.prototype.deleteBlogEntry = function(theListObject){
	var theGUID = SCRIBEFIRE_BLOG.guid;
	
	if (theGUID) {
		var db = SCRIBEFIRE_DB.getDB();
		
		var select = db.createStatement("SELECT id FROM "+SCRIBEFIRE_DB.blogsTable+" WHERE guid=?1 LIMIT 1");
		select.bindStringParameter(0, theGUID);
		
		var blogId = 0;
		
		try {
			while (select.executeStep()) {
				blogId = select.getInt32(0);
			}
		} catch (e) {
			SFLOG(e);
		} finally {
			select.reset();
		}
		
		var del = db.createStatement("DELETE FROM "+SCRIBEFIRE_DB.historyTable+" WHERE blogId=?1");
		del.bindInt32Parameter(0, blogId);
		del.execute();
		
		var del = db.createStatement("DELETE FROM "+SCRIBEFIRE_DB.templatesTable+" WHERE blog_guid=?1");
		del.bindStringParameter(0, theGUID);
		del.execute();
		
		var del = db.createStatement("DELETE FROM "+SCRIBEFIRE_DB.blogsTable+" WHERE guid=?1");
		del.bindStringParameter(0, theGUID);
		del.execute();
		
		SCRIBEFIRE_DB.closeDB(db);
		SCRIBEFIRE_BLOG.guid = "";
		
		// Todo: Just remove the item, don't refresh all blogs.
		gPerformancingUtil.loadBlogs();
	}
	else {
		var alertMessage = performancingUI.getLocaleString('mustselectblog');
		alert(alertMessage);
	}
}

performancingUtil.prototype.getCategories = function (isMT) {
	var tempArray = [];
	
	var checkboxes = document.getElementById("blog-sidebar-listing-categories").getElementsByTagName("checkbox");
	
	for (var i = 0; i < checkboxes.length; i++) {
		if (checkboxes[i].checked) {
			try {
				if (isMT) {
					var temp = {'categoryId' : checkboxes[i].getAttribute('cat') };
				}
				else {
					var temp = checkboxes[i].getAttribute("category_text").replace(/&/g, "&amp;");
				}
			
				tempArray.push(temp);
			} catch (e) {
				alert(e);
			}
		}
	}

	return tempArray;
};

// #### API GETTERS #### 

performancingUtil.prototype.getCategoryList = function(){
	gPerformancingUtil.clearCheckListOut('blog-sidebar-listing-categories');

	if (SCRIBEFIRE_BLOG.serviceObject.standard) {
		var myResponse = SCRIBEFIRE_BLOG.serviceObject.getCategoryList();
		var localeString2 = performancingUI.getLocaleString('notavailable');
		
		if (myResponse) {
			if (typeof myResponse == 'object') {
				performancing_xmlcall.processReturnData(myResponse, "categorycall");
			}
			else {
				var localeString = performancingUI.getLocaleString('fetchingcategories');
				gPerformancingUtil.addCategory(localeString, 0, localeString2, 'label' );
				performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'categorycall', "", "", performancing_xmlcall.processData);
			}
		}
		else {
			//Disable the category section
			var localeString = performancingUI.getLocaleString('nocategoriesavailable');
			gPerformancingUtil.addCategory(localeString, 0, localeString2, 'label' );
		}
	}
	else {
		// MySpace, Tumblr, etc.
		SCRIBEFIRE_BLOG.serviceObject.populateCategoryChecklist();
	}
}

performancingUtil.prototype.getBlogPages = function () {
	gPerformancingUtil.clearListOut('scribefire-pages-list');
	
	if (SCRIBEFIRE_BLOG.supportsPages) {
		var myResponse = SCRIBEFIRE_BLOG.serviceObject.getPages();
		performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'pagescall', null, SCRIBEFIRE_BLOG.guid, performancing_xmlcall.processData);
	}
};

performancingUtil.prototype.getBlogHistory = function(theGUID){
	if (!theGUID) {
	    theGUID = SCRIBEFIRE_BLOG.findGUID();
	    
    	if (!theGUID) {
    		SFLOG("No blog: getBlogHistory");
    		return;
    	}
	}
	
	gPerformancingUtil.clearListOut('scribefire-entries-list');
	
	try {
		SCRIBEFIRE_BLOG.guid = theGUID;
	} catch (noBlog) {
		gPerformancingUtil.getBlogHistory();
		return;
	}
	
	if (SCRIBEFIRE_BLOG.serviceObject.standard) {
		var localeString = performancingUI.getLocaleString('notavailable');
		
		var myResponse = SCRIBEFIRE_BLOG.serviceObject.getRecentPosts();
		
		if (myResponse) {
			var localeString2 = performancingUI.getLocaleString('loading');
			gPerformancingUtil.addItemToList([localeString2], 0, 'scribefire-entries-list', null, localeString, "" );
			performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'historycall', null, SCRIBEFIRE_BLOG.guid, performancing_xmlcall.processData);
		}
	}
	else {
		SCRIBEFIRE_BLOG.serviceObject.populateHistoryList();
	}
}

performancingUtil.prototype.publish = function ({isEdit:isEdit, isPage:isPage, draft:isDraft, title:theTitle, content:theContent, slug:slug}) {
    if (!gTempPost) performancingUI.onServerSend();
    
    function publishStep2() {
        if (SCRIBEFIRE_BLOG.guid) {
    		if (!theContent) {
    			theContent = performancingEditor.getContent();
    		}
		
    		if (theContent){
   				theContent = theContent.replace(/<hr\s*class=['"]jump['"]\s*\/?>/g, "<!-- more -->");
			
    			if( theTitle || performancingUI.checkSubjectEmpty() ){
    				gIsDraft = isDraft;
				
    				if (!theTitle) {
    					theTitle = performancingEditor.getTitle();
    				}
				
    				theContent = gPerformancingUtil.getXHTML(theContent);
				
    				// Add technorati tags here.
    				if (gPerformancingUtil.prefs.getBoolPref("extra.inserttechnorati")) {
    					theContent = theContent.replace(/<p class="technorati-tags">[\s\S]*<\/p>/g, "");
					
    					var tags = document.getElementById("performancing-technorati-tags").value;
					
    					if (tags) {
    						tags = tags.split(",");
						
    						for (var i = 0; i < tags.length; i++) {
    							tags[i] = tags[i].replace(/^\s+|\s+$/g, "");
    							tags[i] = '<a href="http://technorati.com/tag/'+encodeURIComponent(tags[i])+'" rel="tag">'+tags[i]+'</a>';
    						}
						
    						var tagString = tags.join(", ");
    						theContent += "\n\n"+'<p class="technorati-tags">' + tagString + '</p>';
    					}
    				}
				
    				// Do the & stuff here.
    //				theContent = theContent.replace(/&amp;/g, '&');
    //				theContent = theContent.replace(/&/g, '&amp;');
    //				theContent = theContent.replace(/&amp;([^\s&;]+);/g, "&$1;");
    				theContent = theContent.replace(/<wbr>/gi, "<wbr/>");
				
				    var text = theContent;
				    
				    if (text.match(/<pre[^>]*>/i)) {
                		try {
                			var allMatches = text.match(/<pre[^>]*>([\s\S]+?)<\/pre>/img);//[1];

                			for (var i = 0; i < allMatches.length; i++) {
                			    var currentMatch = allMatches[i];

                    			var toReplace = currentMatch.match(/<pre[^>]*>([\s\S]+?)<\/pre>/im)[1];

                    			var replaced = toReplace.replace(/</g, "&lt;");
                    			replaced = replaced.replace(/>/g, "&gt;");

                    			// $ is a special backreference char.
                    			replaced = replaced.replace(/\$/g, "$$$$");

                    			text = text.replace(toReplace, replaced);
                		    }
                		} catch (e) {
                		}
                	}
                	
                	theContent = text;
				
    				// Add the styles to the actual post
    				if (!isEdit) {
    					var theStyles = performancingEditor.getCurrentEditorDocument().getElementById("performancing-message-source").getAttribute('style');
    					if (theStyles) {
    						theContent = '<div style="' + theStyles + '">' + theContent + "</div>";
    					}
    				}
				
    				// If LiveJournal, add Title
    				if (SCRIBEFIRE_BLOG.type == 'livejournal_com'){
    					theContent = "<title>" + theTitle + "</title>" + theContent;
    				}
				
    				var publishThisPost = 1;
    				var useBool =  gPerformancingUtil.useBooleanPublish(SCRIBEFIRE_BLOG.type, SCRIBEFIRE_BLOG.useBoolean);
				
    				if (isDraft){
    					publishThisPost = 'bool0';
    					if (useBool) {
    						publishThisPost = false;
    					}
    				}
    				else {
    					publishThisPost = 'bool1';
    					if (useBool) {
    						publishThisPost = true;
    					}
    				}
				
    				if (isEdit) {
    					var theDateCreated = performancingEditor.getDateCreatedForRepublish();
    				}
    				else {
    					var theDateCreated = performancingEditor.getDateCreatedForNewPost();
    				}
				
    				if (isPage) {
    					if (isEdit) {
    						var myResponse = SCRIBEFIRE_BLOG.serviceObject.editPost(thePostID, theTitle, theContent, [], theDateCreated, publishThisPost, tags, null, slug);
    					}
    					else {
    						var myResponse = SCRIBEFIRE_BLOG.serviceObject.newPage(theTitle, theContent, theDateCreated, publishThisPost, slug);
    					}

    					if (myResponse) {
    						performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'newpagecall', null, "", performancing_xmlcall.processData);
    					}
    				}
    				else {
    					var catArray1 = gPerformancingUtil.getCategories();
						
    					var tags = document.getElementById("input-tags").value;
				
    					if (isEdit) {
    						var myResponse = SCRIBEFIRE_BLOG.serviceObject.editPost(thePostID, theTitle, theContent, catArray1, theDateCreated, publishThisPost, tags, null, slug);
    					}
    					else {
    						var myResponse = SCRIBEFIRE_BLOG.serviceObject.newPost(theTitle, theContent, catArray1, theDateCreated, publishThisPost, tags, slug);
    					}
					
    					if (myResponse) {
    						var catArray = gPerformancingUtil.getCategories(true);
							
    						if (isEdit) {
    							performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'editpostcall', catArray, "", performancing_xmlcall.processData);
    						}
    						else {
    							performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'newpostcall', catArray, "", performancing_xmlcall.processData);
    						}
    					}
    				}
    			}
    			else {
    			    performancingUI.onProgressOff();
                }
    		}
    		else {
    		    performancingUI.onProgressOff();
            }
    	}
    	else{
    	    performancingUI.onProgressOff();
            
    		var localeString = performancingUI.getLocaleString('pleaseselectablogs');
    		alert(localeString);
    	}
    }
    
    if (isEdit) {
		var thePostID = performancingEditor.getCurrentEditorWindow().pffEditorUI.entryId;
	}

    performancingEditor.prepContent(publishStep2);
};

performancingUtil.prototype.useBooleanPublish = function(aBlogType, usesBoolean){
    if(usesBoolean.toString() == 'true'){
        return true;
    }else{
        switch (aBlogType.toString()) { //this._fareWatcherState = FAREWATCHER_STATE_ERROR;
                case 'drupal_cust':
                case 'performancing_com':
                    return true;
                    break;
                    
                default:
                    return false;
                    break;
        }
    }
    return false;
}

performancingUtil.prototype.setCategoryList = function(aPostID, catArray){
    var myResponse = SCRIBEFIRE_BLOG.serviceObject.setPostCategories(aPostID, catArray);
    
    if(myResponse){
        performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'setcategoriescall', null, null, performancing_xmlcall.processData );
    }

    // Now publish it! This fixes a typepad category bug
    // gPerformancingUtil.publishThePost(gLastPostID);
}

performancingUtil.prototype.publishThePost = function(aPostID){
    var myResponse = SCRIBEFIRE_BLOG.serviceObject.publishPost(aPostID);
    
    if (myResponse){
        performancing_xmlcall.sendCommand(SCRIBEFIRE_BLOG.api, myResponse, 'publishPost', null, null, performancing_xmlcall.processData );
    }
}

performancingUtil.prototype.deleteHistoryItem = function(postId){
	// Only called when the "Delete this post" button is pressed.
	
	if (!postId) var confirmText = performancingUI.getLocaleString("deletepostconfirm", []);
	
	if (postId || confirm(confirmText)){
		if (postId) {
			var thePostID = postId;
		}
		else {
			var thePostID = performancingEditor.getEntryId();
		}	
		
	    try{
	        var myResponse = SCRIBEFIRE_BLOG.serviceObject.deletePost(thePostID);
			
			if(myResponse){
				var req = new XMLHttpRequest();
				req.open("POST", SCRIBEFIRE_BLOG.api, true);
				req.setRequestHeader("Content-Length", myResponse.length);
				req.setRequestHeader("Content-Type", "text/xml");
				
				req.onreadystatechange = function () {
					if (req.readyState == 4) {
						if (req.status >= 200 && req.status <= 202) {
							performancing_xmlcall.processData(req.responseText, null, 'deletehistorycall');
						}
						else {
							alert(performancingUI.getLocaleString("deletePostError", [ req.responseText ]));
						}
					}
				};
				
				req.send(myResponse);
	        }
	    }catch(e){
	        var localeString = performancingUI.getLocaleString('selectitemtodelete', [e]);
	        alert(localeString);
	    }
    }
}

performancingUtil.prototype.deletePage = function(){
	if (confirm(performancingUI.getLocaleString("deletepageconfirm"))) {
		var thePostID = performancingEditor.getEntryId();

		try {
			var myResponse = SCRIBEFIRE_BLOG.serviceObject.deletePage(thePostID);
			
			if (myResponse) {
				var req = new XMLHttpRequest();
				req.open("POST", SCRIBEFIRE_BLOG.api, true);
				
				req.onreadystatechange = function () {
					if (req.readyState == 4) {
						if (req.status >= 200 || req.status <= 202) {
							gPerformancingUtil.getBlogPages();
							alert(performancingUI.getLocaleString('pagedeleted'));
						}
						else {
							alert(performancingUI.getLocaleString("deletePageError", [ req.responseText ]));
						}
					}
				};
				
				req.send(myResponse);
			}
			
			performancingEditor.closeTab();
		} catch(e) {
			var localeString = performancingUI.getLocaleString('selectitemtodelete', [e]);
			alert(localeString);
		}
	}
}
